<template>
  <div>
    <template>

      <el-button type="text" @click="dialogVisible = true"> 新增</el-button>

      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose">
        <el-form ref="form" :model="product" :rules="rules" label-width="200px">

          <el-form-item label="商品名称" prop="productName">
            <el-input v-model="product.productName"></el-input>
          </el-form-item>

          <el-form-item label="商品价格" prop="price">
            <el-input v-model="product.price"></el-input>
          </el-form-item>
          <el-form-item label="商品图片" prop="imgUrl">
            <el-upload
              class="avatar-uploader"
              action="http://localhost:8080/upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img v-if="product.imgUrl" :src="product.imgUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="上架日期" prop="createTime">
            <el-date-picker
              v-model="product.createTime"
              type="date"
              placeholder="选择日期">
            </el-date-picker>
          </el-form-item>


          <el-form-item>
            <el-button type="primary" @click="onSubmit('form')">提交</el-button>
            <el-button>取消</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>

      <el-table
        :data="tableData"
        border
        style="width: 100%">
        <el-table-column
          prop="id"
          label="Id"
          width="200">
        </el-table-column>

        <el-table-column
          prop="productName"
          label="商品名称"
          width="200">
        </el-table-column>
        <el-table-column
          prop="price"
          label="商品价格"
          width="200">
        </el-table-column>

        <el-table-column
          prop="imgUrl"
          label="商品图片"
          width="300">
          <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.imgUrl"
              fit="fit"></el-image>
          </template>
        </el-table-column>
        <el-table-column

          prop="createTime"
          label="创建日期"
          width="150">
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="300">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small">删除</el-button>
            <el-button type="text" @click="upd(scope.row)" size="small">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页插件-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page=params.page
        :page-sizes="[2, 4, 6, 8]"
        :page-size=params.size
        layout="total, sizes, prev, pager, next, jumper"
        :total=total>
      </el-pagination>
    </template>
  </div>
</template>

<script>
  export default {
    name: "student",
    data() {
      return {
        tableData: [],
        product: {},
        dialogVisible: false,
        params:{
          page:1,
          size:2,
        },
        //表单验证
        rules: {
          productName: [{required: true, message: '请输入商品名称', trigger: 'blur'},
            {min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur'}],
          price: [{required: true, message: '请输入价格', trigger: 'blur'},],
          imgUrl:[{required:true,message:'图片不能为空',trigger:'blur'}],
          createTime:[{required:true,message:'请选择日期',trigger:'blur'}],
        },
      //分页
        total:2
      }
    },
    methods: {
      findAll: function () {
        this.$axios.get("product/findAll?page="+this.params.page+"&size="+this.params.size+"").then(res => {
          this.tableData = res.data.data;
          this.total=res.data.total
        })
      },
      handleClick: function (row) {
        this.$axios.get("product/deleteById?id=" + row.id).then(res => {
          this.findAll()
        })
      },
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {
          });
      },
      onSubmit: function (formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$axios.post("/product/updateOrSave", this.product).then(res => {

              this.dialogVisible = false;
              this.findAll();

            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });

      },
      upd: function (row) {
        this.product = row;
        this.dialogVisible = true;

      },
      handleAvatarSuccess(res, file) {
        this.product.imgUrl = res;
        this.product.imgUrl = URL.createObjectURL(file.raw);
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },

      //分页方法
      handleSizeChange(val) {
       this.params.size=val
        this.findAll()
      },
      handleCurrentChange(val) {
       this.params.page=val
        this.findAll()
      }


    },
    mounted() {
      this.findAll();
    }
  }
</script>

<style scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
